package com.daocao.yfb.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import butterknife.BindView;
import butterknife.ButterKnife;

import com.daocao.yfb.R;
import com.daocao.yfb.activity.OrderDetailActivity;
import com.daocao.yfb.activity.SelectAccountActivity;
import com.daocao.yfb.activity.SureOrderActivity;
import com.daocao.yfb.api.model.OrderListModel;
import com.daocao.yfb.util.DateTime;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class MyOrderAdapter extends ItemAdapter<OrderListModel.DatasBean, MyOrderAdapter.DefaultHolder> {


    private Context context;


    public MyOrderAdapter(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_order_my, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        OrderListModel.DatasBean item = getItem(position);
        holder.layoutDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (item.status.equals("-1")) {

                } else if (item.status.equals("0")) {
                    Intent intent = new Intent(context, SureOrderActivity.class);
                    intent.putExtra("title", item.name);
                    intent.putExtra("money", item.money);
                    intent.putExtra("id", item.id);
                    intent.putExtra("icon", item.icon);
                    intent.putExtra("relate_id", item.relate_id);
                    intent.putExtra("order_number", item.order_number);
                    intent.putExtra("num", item.num);
                    intent.putExtra("relate_account", item.relate_account);
                    context.startActivity(intent);
                    ((Activity) context).overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                } else {
                    Intent intent = new Intent(context, OrderDetailActivity.class);
                    intent.putExtra("order_number", item.order_number);
                    intent.putExtra("relate_id", item.relate_id);
                    context.startActivity(intent);
                    ((Activity) context).overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                }
            }
        });
        holder.layoutEditId.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (item.status.equals("0") || item.status.equals("2") || item.status.equals("3")) {
                    Intent intent = new Intent(context, SelectAccountActivity.class);
                    intent.putExtra("order_number", item.order_number);
                    context.startActivity(intent);
                    ((Activity) context).overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                }
            }
        });

        Glide.with(context).load(item.icon).into(holder.imgIcon);
        holder.txtStatus.setText(getStatus(item.status));
        holder.txtMoney.setText("￥" + item.money);
        holder.txtTime.setText(DateTime.toDate(item.created_at * 1000));
        holder.txtTitle.setText(item.name);
        holder.txtName.setText(item.merchant_name);
        if (item.relate_account.equals("0")) {
            holder.txtId.setText("云联惠ID:未绑定");
            holder.txtId.setTextColor(0xFFF05454);
            holder.txtBind.setVisibility(View.VISIBLE);
        } else {
            holder.txtId.setText("云联惠ID：" + item.relate_account);
            holder.txtId.setTextColor(0xFF999999);
            holder.txtBind.setVisibility(View.GONE);
        }
        holder.txtNum.setText("x" + item.num);
    }

    public String getStatus(String status) {
        switch (status) {
            case "0":
                return "待支付";
            case "1":
                return "已到账";
            case "-1":
                return "交易关闭";
            case "2":
                return "待发货";
            case "3":
                return "发货中";
            case "4":
                return "已发货";
            default:
                return "";
        }
    }


    //订单状态（0：待支付，1：完成、已到账，-1：失败、交易关闭，2：已付款、待发货，3：发货中，4,：已发货）    状态为0、2、3的时候可以修改云联惠收货账号
    class DefaultHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.txt_bind)
        TextView txtBind;
        @BindView(R.id.txt_name)
        TextView txtName;
        @BindView(R.id.img_icon)
        ImageView imgIcon;
        @BindView(R.id.txt_title)
        TextView txtTitle;
        @BindView(R.id.txt_id)
        TextView txtId;
        @BindView(R.id.txt_time)
        TextView txtTime;
        @BindView(R.id.txt_status)
        TextView txtStatus;
        @BindView(R.id.txt_money)
        TextView txtMoney;
        @BindView(R.id.txt_num)
        TextView txtNum;
        @BindView(R.id.layout_total)
        LinearLayout layoutTotal;
        @BindView(R.id.layout_name)
        RelativeLayout layoutName;
        @BindView(R.id.layout_detail)
        RelativeLayout layoutDetail;
        @BindView(R.id.layout_edit_id)
        RelativeLayout layoutEditId;

        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnSelectItem {
        void onClick(String id);
    }
}
